#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution
{
public:
    vector<string> removeAnagrams(vector<string> &words)
    {
        /**
         * @brief has problem?
         * 
         */
        auto isAnagram = [](const string &a, const string &b) -> bool
        {
            if (a.length() != b.length())
            {
                return false;
            }
            int sum = 0;
            for (int s1 : a)
            {
                sum += s1;
            }
            for (int s2 : b)
            {
                sum -= s2;
            }
            return sum == 0;
        };
        for (int i = 0; i < words.size() - 1; i++)
        {
            if (isAnagram(words[i], words[i + 1]))
            {
                words.erase(words.begin() + i + 1);
                i--;
            }
        }
        return words;
    }

    vector<string> removeAnagrams_2(vector<string> &words)
    {
        vector<string> res = {words[0]}; // 结果数组
        int n = words.size();
        // 判断两个单词是否为字母异位词
        auto compare = [](const string &word1, const string &word2) -> bool
        {
            vector<int> freq(26);
            for (char ch : word1)
            {
                ++freq[ch - 'a'];
            }
            for (char ch : word2)
            {
                --freq[ch - 'a'];
            }
            return all_of(freq.begin(), freq.end(), [](int x)
                          { return x == 0; });
        };

        for (int i = 1; i < n; ++i)
        {
            if (compare(words[i], words[i - 1]))
            {
                continue;
            }
            res.push_back(words[i]);
        }
        return res;
    }
};
int main()
{
    /**
     * @brief 
     * {"truqjvrb","trbuvjqr","ubqtrrjv","tqbujrvr","qrjubrtv","bjvtrrqu","rjutqrvb","rrjqtbvu","vbujtqrr","rtrjvqbu","bqrjuvrt","tqjvburr","vrtjbqru","vqrrujtb","vuqbtrrj","vjrtqbru","rutbqvrj","bvrtjuqr","rrtqbjvu","rjrvbuqt","tqvrburj","rjtrvqub","jutrbqrv","jrvrtubq","bjqrurvt","jvrruqbt","jvburrqt","jvurqbtr","vujbtrrq","qrtubjvr","vbrrtqju","rvurqtbj","rvbjqtru","tjuqbrvr","jtrrubqv","qbrtvruj","urjtbqvr","rurqbjtv","tbjrvuqr","turbjqvr","vqbjrrut","tjvurqbr","turjqrvb","jvubrrqt","bqjtrrvu","tjvrruqb","jbuvrqrt","btqrvjur","juqbrvrt","qrjvbtru","qbjtuvrr","rjtubvqr","rjrbuvqt","qvbjtrru","rvutjbqr","urqtjrvb","qurrjtvb","vrjuqtrb","tuqrjbrv","ubjrqrtv","qutrrbvj","rqjrubvt","vsuokmjq","sujmoqkv","juoksqvm","vqkojmus","qkmsvjou","qmvjosku","ujmkovqs","qvjumkso","kb","kb","vmuu","muvu","mvuu","mvuu","umuv","muvu","uuvm","umuv","vumu","uumv","ldpyqa","aylqpd","ypaqdl","plydqa","xhm","yrsqlm","mqsyrl","yrqlsm","wwqmyph","qymhwwp","wpwhqym","wmqyphw","wyqmpwh","ypwhmwq","wyqwphm","afypmtiwhj"}
     * truqjvrb kb vmuu ldpyqa xhm yrsqlm wwqmyph afypmtiwhj 
     * truqjvrb vsuokmjq kb vmuu ldpyqa xhm yrsqlm wwqmyph afypmtiwhj 
     * 
     */
    Solution s;
    vector<string> words = {"truqjvrb","trbuvjqr","ubqtrrjv","tqbujrvr","qrjubrtv","bjvtrrqu",
    "rjutqrvb","rrjqtbvu","vbujtqrr","rtrjvqbu","bqrjuvrt","tqjvburr","vrtjbqru",
    "vqrrujtb","vuqbtrrj","vjrtqbru","rutbqvrj","bvrtjuqr","rrtqbjvu","rjrvbuqt",
    "tqvrburj","rjtrvqub","jutrbqrv","jrvrtubq","bjqrurvt","jvrruqbt","jvburrqt",
    "jvurqbtr","vujbtrrq","qrtubjvr","vbrrtqju","rvurqtbj","rvbjqtru","tjuqbrvr",
    "jtrrubqv","qbrtvruj","urjtbqvr","rurqbjtv","tbjrvuqr","turbjqvr","vqbjrrut",
    "tjvurqbr","turjqrvb","jvubrrqt","bqjtrrvu","tjvrruqb","jbuvrqrt","btqrvjur",
    "juqbrvrt","qrjvbtru","qbjtuvrr","rjtubvqr","rjrbuvqt","qvbjtrru","rvutjbqr",
    "urqtjrvb","qurrjtvb","vrjuqtrb","tuqrjbrv","ubjrqrtv","qutrrbvj","rqjrubvt",
    "vsuokmjq","sujmoqkv","juoksqvm","vqkojmus","qkmsvjou","qmvjosku","ujmkovqs",
    "qvjumkso","kb","kb","vmuu","muvu","mvuu","mvuu","umuv","muvu","uuvm","umuv","vumu",
    "uumv","ldpyqa","aylqpd","ypaqdl","plydqa","xhm","yrsqlm","mqsyrl","yrqlsm","wwqmyph",
    "qymhwwp","wpwhqym","wmqyphw","wyqmpwh","ypwhmwq","wyqwphm","afypmtiwhj"};
    vector<string> ans = s.removeAnagrams_2(words);
    for (string s : ans)
    {
        cout << s << " ";
    }
    cout << endl;
    system("pause");
    return 0;
}
